package com.example.lib_base.base.utils;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.BuildConfig;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

/**
 * 自定义 打印日志工具
 * <br />
 * {@link #mIsAbleLog} 控制打印开关<br />
 * Created by Administrator on 2015/3/4.
 */
public final class MyLogUtil {
    /**
     * 默认日志tag <br/>
     * created at 2015/12/27 14:46
     */
    private static final String TAG = "myLogUtil";

    private static LogAdapter newClassDiskLogAdapter;

    /**
     * 默认提供打印服务
     */
    private static boolean mIsAbleLog = true;

    public static final int V = 0;
    public static final int I = 1;
    public static final int D = 2;
    public static final int W = 3;
    public static final int E = 4;

    public static int logLevel = 0;

    static {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // //（可选）是否显示线程信息。 默认值为true
                .methodCount(0)          // （可选）要显示的方法行数。 默认2
                .methodOffset(7)        // （可选）设置调用堆栈的函数偏移值，0的话则从打印该Log的函数开始输出堆栈信息，默认是0
//                    .logStrategy(customLog) //（可选）更改要打印的日志策略。 默认LogCat
                .tag("MyLogUtil")    //（可选）每个日志的全局标记。 默认PRETTY_LOGGER（如上图）
                .build();
        newClassDiskLogAdapter = new LogAdapter();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        Logger.addLogAdapter(newClassDiskLogAdapter);
    }


    public static void log(LogLevel level, String tag, String message) {
        switch (level.ordinal()) {
            case V:
                v(tag, message);
                break;
            case I:
                i(tag, message);
                break;
            case D:
                d(tag, message);
                break;
            case W:
                w(tag, message);
                break;
            case E:
                e(tag, message);
                break;
            default:
                d(tag, message);
                break;
        }
    }


    public static void log(LogLevel level, String message) {
        switch (level.ordinal()) {
            case V:
                v(message);
                break;
            case I:
                i(message);
                break;
            case D:
                d(message);
                break;
            case W:
                w(message);
                break;
            case E:
                e(message);
                break;
            default:
                d(message);
                break;
        }
    }

    /**
     * 打印 verbose级别
     *
     * @param tag
     * @param msg
     */
    public static void v(String tag, String msg) {
        if (mIsAbleLog && logLevel <= V) {
            Logger.t(tag).v(msg);
        }
    }

    /**
     * 打印 verbose级别
     *
     * @param msg
     */
    public static void v(String msg) {
        if (mIsAbleLog && logLevel <= V) {
            Logger.v(msg);
        }
    }

    /**
     * 打印 info级别
     *
     * @param tag
     * @param msg
     */
    public static void i(String tag, String msg) {
        if (mIsAbleLog && logLevel <= I) {
            Logger.t(tag).i(msg);
        }
    }

    public static void i(String msg) {
        if (mIsAbleLog && logLevel <= I) {
            Logger.i(msg);
        }
    }

    /**
     * 打印 debug级别
     *
     * @param tag
     * @param msg
     */
    public static void d(String tag, String msg) {
        if (mIsAbleLog && logLevel <= D) {
            Logger.t(tag).d(msg);
        }
    }

    /**
     * 打印 debug级别
     *
     * @param msg
     */
    public static void d(String msg) {
        if (mIsAbleLog && logLevel <= D) {
            Logger.d(msg);
        }
    }

    /**
     * 打印 warn级别
     *
     * @param tag
     * @param msg
     */
    public static void w(String tag, String msg) {
        if (mIsAbleLog && logLevel <= W) {
            Logger.t(tag).w(msg);
        }
    }

    /**
     * 打印 warn级别
     *
     * @param msg
     */
    public static void w(String msg) {
        if (mIsAbleLog && logLevel <= W) {
            Logger.w(msg);
        }
    }

    /**
     * 打印 warn级别
     *
     * @param tag
     * @param msg
     */
    public static void w(String tag, String msg, Throwable e) {
        if (mIsAbleLog && logLevel <= W) {
            Logger.t(tag).e(e, msg);
        }
    }


    /**
     * 打印 error级别
     *
     * @param tag
     * @param msg
     */
    public static void e(String tag, String msg) {
        if (mIsAbleLog && logLevel <= E) {
            Logger.t(tag).e(msg);
        }
    }

    /**
     * 打印 error级别
     *
     * @param msg
     */
    public static void e(String msg) {
        if (mIsAbleLog && logLevel <= E) {
            Logger.e(msg);
        }
    }

    /**
     * 打印 error级别
     *
     * @param tag
     * @param msg
     * @param e
     */
    public static void e(String tag, String msg, Throwable e) {
        if (mIsAbleLog && logLevel <= E) {
            Logger.t(tag).e(e, msg);
        }
    }

    public static void json(String tag, String msg) {
        if (mIsAbleLog && logLevel <= D) {
            Logger.t(tag).json(msg);
        }
    }

    public static void json(String msg) {
        if (mIsAbleLog && logLevel <= D) {
            Logger.json(msg);
        }
    }

    /**
     * 设置打印等级
     *
     * @param level
     */
    public static void setLogLevel(int level) {
        logLevel = level;
    }


    /**
     * 设置是否需要打印日志
     *
     * @param isAbleLog
     */
    public static void setIsAbleLog(boolean isAbleLog) {
        mIsAbleLog = isAbleLog;
    }

    public static void setIsAbleFileLog(boolean isAbleFileLog) {
        newClassDiskLogAdapter.setLogIsAble(isAbleFileLog);
    }

}
